package com.mec.ServerCluser.core;

import com.mec.ServerCluser.Exception.SameAppServerAndServiceRegistryException;

import java.net.Socket;

import static com.mec.ServerCluser.core.ENetConstance.REGISTRY_CENTER;

public class RegistryAppServerConversation extends Communication {
    private RegistryCenter registryCenter;
    private Socket appServer;
    private String appServerIp;
    private int appServerPort;
    private String serviceName;

    public RegistryAppServerConversation(RegistryCenter registryCenter, Socket appServer) {
        super(appServer);
        this.registryCenter = registryCenter;
        this.appServer = appServer;
    }

    @Override
    protected void dealNetMessage(NetMessage message) {
        ENetCommand command = message.getCommand();

        /**
         * 分发器需求分析
         * 主要就是根据消息中的命令，和消息内容进行不同的操作。
         * 所以在分发器层，我们方法的接口给出（String command, String message）
         * 然后在给出一个类，将分发器层封装起来，其中就封装了Conversation对象
         * 为各种操作创造基础。
         */

        switch(command){
            case APPSERVER_REQUEST_REGISTRY :
                //接收注册相关的信息，并对其进行注册
                String info = message.getParameter();
                int beginIndex = 0;
                int keyIndex = info.indexOf("&");
                serviceName = info.substring(beginIndex, keyIndex);
                beginIndex = keyIndex + 1;
                keyIndex = info.indexOf("&", beginIndex);
                appServerIp = info.substring(beginIndex, keyIndex);
                System.out.println("appServerIp：" + appServerIp);
                beginIndex = keyIndex + 1;
                keyIndex = info.indexOf("&", beginIndex);
                System.out.println("appServerPort：" + info.substring(beginIndex));
                appServerPort = Integer.valueOf(info.substring(beginIndex));

                try {
                    registryCenter.addServer(serviceName, appServerIp, appServerPort);
                } catch (SameAppServerAndServiceRegistryException e) {
                    //在此处应该向应用服务器发送“重复注册的消息”，并断开与其的链接。
                    send(new NetMessage()
                            .setFrom(REGISTRY_CENTER.toString())
                            .setTo(appServerIp)
                            .setCommand(ENetCommand.REFUSED_ONLINE)
                            .setParameter("服务器已存在，已禁止重复注册。"));
                    e.printStackTrace();
                }
                break;

            case APPSERVER_LOGOUT:
                info = message.getParameter();
                beginIndex = 0;
                keyIndex = info.indexOf("&");
                serviceName = info.substring(beginIndex, keyIndex);
                System.out.println("serviceName：" + serviceName);
                beginIndex = keyIndex + 1;
                keyIndex = info.indexOf("&", beginIndex);
                appServerIp = info.substring(beginIndex, keyIndex);
                beginIndex = keyIndex + 1;
                keyIndex = info.indexOf("&", beginIndex);
                appServerPort = Integer.valueOf(info.substring(beginIndex));
                try {
                    registryCenter.removeServer(serviceName, appServerIp, appServerPort);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                stop();
                break;
        }
    }

    @Override
    protected void peerAbnormalDrop() {
        System.out.println("检测到应用服务器[" + serviceName + "[" +
                appServerIp + "]"+ "]异常掉线！");

        try {
            registryCenter.removeServer(serviceName, appServerIp, appServerPort);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
